<!-- eslint-disable -->
<template>
  <el-card class="box-card">
    <el-table :data="practiceList" border style="width: 100%">
      <el-table-column prop="id" label="预约号" width="180" align="center"></el-table-column>
      <el-table-column prop="nickname" label="学员姓名" width="100" align="center"></el-table-column>
      <el-table-column prop="name" label="课程名" width="100" align="center"></el-table-column>
      <el-table-column prop="coach" label="教练名" width="80" align="center"></el-table-column>
      <el-table-column prop="adress" label="地址" width="80" align="center"></el-table-column>
      <el-table-column prop="img" label="课程图片" width="120" class="imgwrap" align="center">
        <template v-slot="{ row }">
          <img :src="row.img" alt />
        </template>
      </el-table-column>

      <el-table-column prop="time" label="练车时间" width="200" align="center"></el-table-column>
      <el-table-column prop="address" label="操作" align="center">
        <template slot-scope="{ row, index }">
          <el-button
            type="success"
            size="medium"
            icon="el-icon-check"
            title="练车完成"
            :disabled="role == 'user'"
            @click="deleteP(row)"
          ></el-button>
          <el-button
            type="warning"
            size="medium"
            icon="el-icon-edit"
            title="修改学员信息"
            :disabled="role !== 'admin'"
            @click="toUpdate(row)"
          ></el-button>
          <el-button
            type="info"
            size="medium"
            icon="el-icon-message"
            title="查看练车详细信息"
            @click="toInfo(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[4, 6, 8, 10]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>

    <!-- 修改弹出层 -->
    <el-dialog title="修改练车信息" :visible.sync="updateStatus" width="30%">
      <el-form :model="form">
        <el-form-item label="地址" prop="username">
          <el-input v-model="form.adress" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="日期" prop="time">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="form.time"
          ></el-date-picker>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="updateStatus = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 学院员详情 -->
    <el-dialog title="学员详情" :visible.sync="infoStatus" width="50%">
      <el-form :model="practiceInfo">
        <el-form-item style="width: 600px" label="头像">
          <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
          >
            <img v-if="practiceInfo.img" :src="practiceInfo.img" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="practiceInfo.nickname" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="practiceInfo.adress" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-input v-model="practiceInfo.coach" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            type="info"
            placeholder="选择日期"
            v-model="practiceInfo.time"
          ></el-date-picker>
        </el-form-item>
      </el-form>
    </el-dialog>
  </el-card>
</template>

<script>
/* eslint-disable */
import { practiceList, deletePractices, updatePractice, getInfo } from "@/api/practice";
import { mapState } from "vuex";
export default {
  name: "Practice",
  data() {
    return {
      //   infoStatus: false,
      pageNumber: 1, //当前页
      pagesize: 4, //分页的数据
      total: 0, //总页数
      practiceList: [],
      //   updateStatus: false,
      updateStatus: false,
      form: {
        id: null,
        adress: null,
        time: null,
      },
      practiceInfo: {},
      infoStatus: false,
      //   info: {},
    };
  },
  methods: {
    handleSizeChange(val) {
      this.pagesize = val;
      this.getpracticeList();
    },
    handleCurrentChange(val) {
      this.pageNumber = val;
      this.getpracticeList();
    },
    async getpracticeList() {
      let res = await practiceList({
        pageNumber: this.pageNumber,
        pagesize: this.pagesize,
      });
      if (res.code == 200) {
        this.practiceList = res.data;
        this.practiceList.forEach(item => {
            item.time = this.dayjs(item.time).format("YYYY-MM-DD HH:mm:ss")
        });
        this.total = res.totalCount;
      }
    },
    async deleteP(row) {
      let res = await deletePractices(row.id);
      if (res.code == 200) {
        this.getpracticeList();
        this.$message({
          message: res.message,
          type: "success",
        });
      }
    },
    toUpdate(row) {
      this.updateStatus = true;
      this.form.id = row.id;
    },
    async update() {
      let res = await updatePractice(this.form);
      this.updateStatus = false;
      if (res.code == 200) {
        this.getpracticeList();
        this.$message({
          message: res.message,
          type: "success",
        });
        this.form = {};
      }
    },
    async toInfo(row) {
      this.infoStatus = true;
      let res = await getInfo(row.id);
      if (res.code == 200) {
        this.practiceInfo = res.data;
      }
    },
  },
  mounted() {
    this.getpracticeList();
  },
  computed: {
    ...mapState({
      role: (store) => store.user.info.role,
      info: (store) => store.user.info,
    }),
  },
};
</script>

<style scoped>
img {
  width: 100px;
  height: 100px;
  margin-right: 15px;
}
.box-card {
  width: 1200px;
  margin-left: 50px;
  margin-top: 50px;
}
</style>
